<template>
  <div>
    <div class="top-panel__box">
      <el-form :model="dataForm" ref="dataForm" inline size="small" onsubmit="return false">
        <el-form-item>
          <organization-cascader :flag="'id'" v-model="dataForm.organizationId"></organization-cascader>
        </el-form-item>
        <el-form-item>
          <el-input clearable placeholder="员工姓名或工号" v-model="dataForm.freeCondition"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="seachList">
            <fa-icon class="fa-search" />查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row class="table-top">
      <el-table :data="dataList" v-loading="tableLoading" tooltip-effect="light" @select="handleSelectionChange" class="tc-table-b" size="small" ref="tableRef">
        <el-table-column type="selection" width="55" fixed="left" :selectable="checkSelection"></el-table-column>
        <template v-for="(column, index) in ACCEPTOR_HEAD_LIST">
          <el-table-column :prop="column.prop" :label="column.label" :key="index" :width="column.width" :show-overflow-tooltip="true" />
        </template>
      </el-table>
      <el-row class="pagination-top">
        <pagination :pageNum.sync="dataForm.pageNum" :curSizes.sync="dataForm.pageSize" :total="total" @getList="getList" />
      </el-row>
    </el-row>
    <el-row class="table-btn">
      <el-button type="primary" @click="close" plain>取消</el-button>
      <el-button type="primary" @click="submit" size="small">
        <fa-icon class="fa-check" />确定</el-button>
    </el-row>
  </div>
</template>

<script>
import {mapState} from 'vuex'
import OrganizationCascader from '@/components/organizationCascader'
import api from '../api'
import { CONSTANT_OBJ } from '@/utils/constant'
// 分派受理人
export const ACCEPTOR_HEAD_LIST = [
  { prop: 'realName', label: '员工姓名' },
  { prop: 'userName', label: '工号' },
  { prop: 'organizationName', label: '所属机构' },
  { prop: 'newStatus', label: '账号状态' }
]
export default {
  components: { OrganizationCascader },
  data () {
    return {
      ACCEPTOR_HEAD_LIST,
      tableLoading: false,
      dataForm: {
        organizationId: '',
        freeCondition: '',
        researchFlag: 'Y',
        pageNum: 1,
        pageSize: 10
      },
      dataList: [],
      selectedList: '',
      total: 0
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    // 查询列表
    getList (val) {
      this.dataForm.pageNum = val || this.dataForm.pageNum
      this.tableLoading = true
      api.queryAcceptorList(this.dataForm).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.dataList = this.disoseStatus(data.list) || []
          this.total = parseInt(data.total) || 0
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    /**
    * @description 用户状态为正常或冻结时显示可选
    */
    checkSelection (row) {
      return row.userStatus === 'NORMAL' || row.userStatus === 'FROZEN'
    },
    disoseStatus (list) {
      return list.map(ele => {
        this.dicts.user_status.childList.forEach(v => {
          if (ele.userStatus === v.dictNo) {
            ele.newStatus = v.dictName
          }
        })
        return ele
      })
    },
    handleSelectionChange (selection, row) {
      this.$refs.tableRef.clearSelection()
      if (selection.length === 0) {
        return
      }
      this.$refs.tableRef.toggleRowSelection(row, true)
      console.log(row)
      this.selectedList = row.userId
    },
    submit () {
      let data = this.selectedList
      console.log(data, '提交')
      if (!data) {
        this.$message({
          message: '请选择分派人员',
          type: 'warning'
        })
        return false
      }
      this.$emit('updataSuccess', data)
    },
    seachList () {
      this.getList()
    },
    close () {
      this.$emit('close', false)
    }
  },
  computed: {
    ...mapState({
      dicts: state => state.common.dicts
    })
  }
}
</script>

<style lang="less">
.table-btn {
  margin: 20px;
  text-align: right;
}
.tc-table-b thead .el-table-column--selection.is-leaf .el-checkbox{
  display: none;
}
</style>
